﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraTab;
using MicropleSoft.Controls.DialogBox;
using MicropleSoft.WmsApp.Library;
using MicropleSoft.WmsApp.Library.ApplicationObserver;
using MicropleSoft.WmsApp.Library.LookUpInterface;
using MicropleSoft.WmsApp.Library.ServiceFacade;

namespace MicropleSoft.Controls
{
    public partial class DataBinderPanelBase : DevExpress.XtraEditors.XtraUserControl, IDataBinderPanel
    {
        public DataBinderPanelBase()
        {
            InitializeComponent();
        }

        public void ExecuteDataBind(object obj)
        {
            ExecuteDataBindChildren(obj, this);
        }

// ReSharper disable MemberCanBeMadeStatic.Local
        private void ExecuteDataBindChildren(object obj,Control parentControl)
// ReSharper restore MemberCanBeMadeStatic.Local
        {
            if (parentControl is XXtraTabControl)
            {
                var tabControl = (XXtraTabControl) parentControl;
                foreach (XtraTabPage tabPage in tabControl.TabPages)
                {
                    ExecuteDataBindChildren(obj, tabPage);
                }
            }
            else
            {
                foreach (Control control in parentControl.Controls)
                {
                    if (control is XXtraTabControl)
                    {
                        var tabControl = (XXtraTabControl)control;
                        foreach (XtraTabPage tabPage in tabControl.TabPages)
                        {
                            ExecuteDataBindChildren(obj, tabPage);
                        }
                    }
                    else if (control is XPanelControl)
                    {
                        var xPanelControl = (XPanelControl)control;
                        foreach (Control tabPage in xPanelControl.Controls)
                        {
                            ExecuteDataBindChildren(obj, tabPage);
                        }
                    }
                    else if (control is IDataBinder && obj is IBaseDomainObject)
                    {
                        if (control is IDataSource)
                        {
                            //如果是数据源绑定控件，则加载数据源
                            //如果是LookUp，则绑定列，如果是GridLookUp，绑定视图
                            //另外将控件送入观察者容器中
                            //当控件窗体关闭，则从观察者容器中移除
                            //另外当缓存刷新，则将所有的观察者对象刷新一遍

                            var dataSource = (IDataSource)control;

                            if(String.IsNullOrEmpty(dataSource.DataSourceKey))
                            {
                                CustomMessageBox.Show(control.Name + "的DataSourceKey属性没有赋值！");
                                continue;
                            }
                            var baseDataWrap = (BaseDataWrap)dataSource.GetBaseDataProvider().GetBaseDataByKey(dataSource.DataSourceKey);

                            if (control is ILookUpViewHeaderBuilder)
                            {
                                var lookUpViewHeaderBuilder = (ILookUpViewHeaderBuilder)control;
                                lookUpViewHeaderBuilder.BuildColumns(baseDataWrap.LookUpViewHeaderList);
                            }

                            if (dataSource.DataSourceUseCache)
                            {
                                DataSourceControlObserverFactory.CreateDataSourceControlObserver().AddControl(this,
                                                                                                              dataSource.
                                                                                                                  DataSourceKey);
                            }
                        }

                        var complexDataBinder = control as IDataBinder;
                        if (String.IsNullOrEmpty(complexDataBinder.PropertyName))
                            continue;
                        complexDataBinder.ExcuteDataBinding((IBaseDomainObject)obj);
                    }
                }
            }
        }
    }
}
